package com.zmn.oms.business.impl.servtype;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.base.price.common.dto.ProductPriceDRO;
import com.zmn.base.price.common.dto.ProductPriceQuery;
import com.zmn.base.price.common.dto.item.ItemDRO;
import com.zmn.base.price.common.dto.item.ItemPriceQuery;
import com.zmn.base.price.dubbo.interfaces.calculate.ItemPriceRemoteService;
import com.zmn.base.price.dubbo.interfaces.calculate.ProductPriceRemoteService;
import com.zmn.base.product.common.consts.BaseProductConsts;
import com.zmn.base.product.common.dto.product.ec.ProductBaseDRO;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.common.dio.quotation3.ArtificialPromDRO;
import com.zmn.oms.common.dio.quotation3.ArtificialPromListQuery;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.work.modify.*;
import com.zmn.oms.model.entity.serviceitem.OrderServiceItem;
import com.zmn.scm2.common.model.dro.BasePageDRO;
import com.zmn.scm2.common.model.dro.scm.engineer.warehouse.EngineerWarehouseDRO;
import com.zmn.scm2.common.model.query.scm.engineer.warehosue.ScmEngineerWarehouseQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName BeforeQuotationThreeServTypeServiceImpl
 * @description: 计价器3.0之前的服务类型
 * @author: wangyanci
 * @create: 2021-11-29 15:32
 * @Version 1.0
 **/
@Slf4j
@Service
public class BeforeQuotationThreeServTypeServiceImpl extends AbstractServItemService {

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    protected ItemPriceRemoteService itemPriceRemoteService;
    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    private ProductPriceRemoteService productPriceRemoteService;

    @Override
    public ResponseDTO<ServiceItemResultDTO> doGetServiceItem(ServiceItemQueryDTO serviceItemQueryDTO) {
        List<ServiceItemResultDTO> serviceItemResultDTOList = this.doListServiceItem(serviceItemQueryDTO);
        if (CollectionUtil.isNullOrEmpty(serviceItemResultDTOList)) {
            ResponseDTO responseDTO = new ResponseDTO();
            responseDTO.setStatus(StatusConsts.STATUS_ERROR);
            responseDTO.setMessage("无匹配服务项");
            return responseDTO;
        }

        ResponseDTO response = new ResponseDTO();
        response.setStatus(StatusConsts.STATUS_SUCCESS);
        response.setData(serviceItemResultDTOList.get(0));
        return response;
    }

    @Override
    public List<ServiceItemResultDTO> doListServiceItem(ServiceItemQueryDTO serviceItemQueryDTO) {

        Map<Integer, ProductBaseDRO> productBaseDROMap = super.getProductBaseDROMap(Lists.newArrayList(serviceItemQueryDTO.getProductId()));

        // 特殊服务项
        List<ItemDRO> itemDROList = super.listSpecialItemByIdList(serviceItemQueryDTO.getIdList());


        ItemPriceQuery itemPriceQuery = ItemPriceQuery
                .builder()
                .channelId(serviceItemQueryDTO.getChannelId())
                .bizType(serviceItemQueryDTO.getBizType())
                .cityId(serviceItemQueryDTO.getCityId())
                .productId(serviceItemQueryDTO.getProductId())
                .tariffId(serviceItemQueryDTO.getTariffId())
                .brandId(serviceItemQueryDTO.getBrandId())
                .difficultId(serviceItemQueryDTO.getDifficultId())
                .showType(productBaseDROMap.get(serviceItemQueryDTO.getProductId()).getShowType())
                .itemList(serviceItemQueryDTO.getItemIdList())
                .build();

        log.info("itemPriceRemoteService.listItemPriceByQuery itemPriceQuery={}", JSON.toJSONString(itemPriceQuery));
        ResponseDTO<List<ItemDRO>> itemResponseDTO = itemPriceRemoteService.listItemPriceByQuery(itemPriceQuery);
        log.info("itemPriceRemoteService.listItemPriceByQuery itemResponseDTO={}", JSON.toJSONString(itemResponseDTO));
        if (CollectionUtils.isNotEmpty(itemResponseDTO.getData())) {
            itemDROList.addAll(itemResponseDTO.getData());
        }

        // 转换实体
        List<ServiceItemResultDTO> serviceItemResultDTOS = super.itemDRO2ServiceItemResultDTO(itemDROList);
        for (ServiceItemResultDTO serviceItemResultDTO : serviceItemResultDTOS) {
            serviceItemResultDTO.setServItemType(serviceItemQueryDTO.getServItemType());
        }

        // 附加项
        if (serviceItemQueryDTO.isSearchAdditionItem()) {
            ProductBaseDRO productBaseDRO = productBaseDROMap.get(serviceItemQueryDTO.getProductId());
            serviceItemResultDTOS.addAll(super.listAdditionItemByCityAndCateg(serviceItemQueryDTO.getCityId(), productBaseDRO.getServCategId(), productBaseDRO.getCategId(), serviceItemQueryDTO.getServItemType()));
        }

        // 过滤服务项
        super.filterServiceItemByQuery(serviceItemResultDTOS, serviceItemQueryDTO);

        return serviceItemResultDTOS;
    }

    @Override
    ResponseDTO<BasePageDRO<EngineerWarehouseDRO>> doListPageEngineerWarehouseByQueryV2(PartQueryDTO partQueryDTO) {
        ScmEngineerWarehouseQuery query = new ScmEngineerWarehouseQuery();
        query.setEngineerId(String.valueOf(partQueryDTO.getMasterId()));
        query.setProductCategTwoId(partQueryDTO.getCategId());
        query.setServCategId(partQueryDTO.getServCategId());
        query.setPageSize(200);
        log.info("获取工程师配件库存入参[{}]", query);
        ResponseDTO<BasePageDRO<EngineerWarehouseDRO>> responseDTO = engineerWarehouseListRemoteService.listPageEngineerWarehouseByQueryV2(query);
        log.info("获取工程师配件库存出参[{}]", responseDTO);
        return responseDTO;
    }

    @Override
    public List<OrderServiceItem> doFilterServiceItem(List<OrderServiceItem> serviceItems, Integer servItemType, Integer finalPrice) {
        log.info("BeforeQuotationThreeServTypeServiceImpl.doFilterServiceItem");
        return serviceItems;
    }

    @Override
    public BackProductPriceResultDTO doQueryBackProductPrice(BackProductPriceQueryDTO backProductPriceQueryDTO) {
        log.info("BeforeQuotationThreeServTypeServiceImpl.doQueryBackProductPrice backProductPriceQueryDTO={}", backProductPriceQueryDTO);
        Integer productId = Optional.ofNullable(backProductPriceQueryDTO.getProductId()).orElse(backProductPriceQueryDTO.getShowProductId());
        if (NumberUtil.isNullOrZero(productId)) {
            return null;
        }

        ProductPriceQuery productPriceQuery = ProductPriceQuery
                .builder()
                .showType(NumberUtil.isNullOrZero(backProductPriceQueryDTO.getShowProductId()) ? BaseProductConsts.ERP_SHOW_TYPE : BaseProductConsts.EC_SHOW_TYPE)
                .productIdList(Lists.newArrayList(productId))
                .cityId(backProductPriceQueryDTO.getCityId())
                .channelId(backProductPriceQueryDTO.getChannelId())
                .brandId(backProductPriceQueryDTO.getBrandId())
                .bizType(backProductPriceQueryDTO.getBizType())
                .build();
        log.info("productPriceRemoteService.listProductPriceByQuery productPriceQuery={}", JSON.toJSONString(productPriceQuery));
        ResponseDTO<List<ProductPriceDRO>> listResponseDTO = productPriceRemoteService.listProductPriceByQuery(productPriceQuery);
        log.info("productPriceRemoteService.listProductPriceByQuery listResponseDTO={}", listResponseDTO);
        if (CollectionUtil.isNullOrEmpty(listResponseDTO.getData())) {
            return null;
        }
        ProductPriceDRO productPriceDRO = listResponseDTO.getData().get(0);
        if (Objects.isNull(productPriceDRO.getItemDRO())) {
            return null;
        }
        List<ItemDRO> itemDROList = new ArrayList<>(1);
        itemDROList.add(productPriceDRO.getItemDRO());

        List<ServiceItemResultDTO> serviceItemResultDTOS = super.itemDRO2ServiceItemResultDTO(itemDROList);

        ItemDRO itemDRO = productPriceDRO.getItemDRO();
        return BackProductPriceResultDTO
                .builder()
                .productId(itemDRO.getProductId())
                .price(itemDRO.getPrice())
                .unit(itemDRO.getItemUnit())
                .defaultServiceItem(serviceItemResultDTOS.get(0))
                .build();
    }

    @Override
    List<Integer> doQueryServItemTypeIdList(Integer servItemType) {
        return null;
    }

    @Override
    List<ArtificialPromDRO> doQueryArtificialPromList(ArtificialPromQueryDTO query) throws OmsBaseException {
        return null;
    }


}